package com.longcai.piaojia.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.longcai.piaojia.BaseApplication;
import com.longcai.piaojia.R;
import com.longcai.piaojia.adapter.AppointmentAdapter;
import com.longcai.piaojia.conn.AppointmentListGet;
import com.module.pull.PullToRefreshBase;
import com.module.pull.PullToRefreshListView;
import com.zcx.helper.app.AppCallBack;
import com.zcx.helper.bound.BoundView;
import com.zcx.helper.http.AsyCallBack;
import com.zcx.helper.scale.ScaleScreenHelperFactory;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/1/9.
 */
public class MyAppointmentActivity extends BaseActivity implements AppointmentAdapter.EnvaluateIntef {

    @BoundView(R.id.appointment_lv) private PullToRefreshListView pullToRefreshListView;
    private ListView appointment_lv;
    private List<AppointmentListGet.OrderEntity> lists = new ArrayList<>();
    private AppointmentAdapter adapter;
    private AppointmentListGet.OrderListEntity mOrderListEntity;

    private AppointmentListGet appointmentListGet = new AppointmentListGet(new AsyCallBack<AppointmentListGet.OrderListEntity>() {

        @Override
        public void onSuccess(String toast, int type, AppointmentListGet.OrderListEntity orderListEntity) throws Exception {

            if (type != 2) {

                lists.clear();

            }

            mOrderListEntity = orderListEntity;

            lists.addAll(orderListEntity.orderEntityList);

            adapter.notifyDataSetChanged();

        }

        @Override
        public void onFail(String toast, int type) throws Exception {

            Toast.makeText(context, toast, Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onEnd(String toast, int type) throws Exception {

            pullToRefreshListView.onPullUpRefreshComplete();
            pullToRefreshListView.onPullDownRefreshComplete();

            if (type != 2) { //type==0  只有进来时候执行一次

                setLastUpdateTime();

            }

        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_my_appointment);

        setBackTrue();

        setTitleName(getString(R.string.mine_appointment_text));

        pullToRefreshListView.setScrollLoadEnabled(false);
        pullToRefreshListView.setPullLoadEnabled(true);
        pullToRefreshListView.setPullRefreshEnabled(true);

        pullToRefreshListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {

                appointmentListGet.page = 1;
                appointmentListGet.user_id = BaseApplication.BasePreferences.readUserId();
                appointmentListGet.execute(context, false, 1);

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

                if (lists != null && lists.size() < mOrderListEntity.total) {

                    appointmentListGet.page = mOrderListEntity.current_page + 1;
                    appointmentListGet.user_id = BaseApplication.BasePreferences.readUserId();
                    appointmentListGet.execute(context, false, 2);

                } else {

                    pullToRefreshListView.onPullUpRefreshComplete();
                    pullToRefreshListView.onPullDownRefreshComplete();

                }

            }
        });

        appointment_lv = pullToRefreshListView.getRefreshableView();

        appointment_lv.setDivider(ContextCompat.getDrawable(context, R.color.background_bg_color));

        appointment_lv.setDividerHeight(ScaleScreenHelperFactory.getInstance().getWidthHeight(20));

        appointment_lv.setAdapter(adapter = new AppointmentAdapter(this, lists));

        appointment_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Bundle bundle = new Bundle();

                bundle.putString("id", lists.get(position).id);
                bundle.putString("p_id", lists.get(position).p_id);

                startActivity(new Intent(MyAppointmentActivity.this, MyAppointmentDetailActivity.class).putExtras(bundle));

            }
        });


        appointmentListGet.page = 1;
        appointmentListGet.user_id = BaseApplication.BasePreferences.readUserId();
        appointmentListGet.execute(this);

        setAppCallBack(new RefreshAppointment());

    }

    @Override
    public void envaluate(int pos) {

        Bundle bundle = new Bundle();

        bundle.putString("p_id", lists.get(pos).p_id);
        bundle.putString("id", lists.get(pos).id);

        startActivity(new Intent(this, LatestEvaluateActivity.class).putExtras(bundle));

    }

    private void setLastUpdateTime() {

        SimpleDateFormat mDateFormat = new SimpleDateFormat("MM-dd HH:mm");
        pullToRefreshListView.setLastUpdatedLabel(mDateFormat.format(System.currentTimeMillis()));
    }

    public class RefreshAppointment implements AppCallBack {

        public void refresh(){

            appointmentListGet.execute(context);

        }
    }

}
